import { createRouter, createWebHistory } from 'vue-router'
import login from '../views/login/login.vue'
import admin from '../views/home/admin.vue'
import statistics from '../views/home/homePage/statistics.vue'
import pending from '../views/home/homePage/pending.vue'
import product from '../views/home/managementPage/product.vue'
import technician from '../views/home/managementPage/technician.vue'
import users from '../views/home/managementPage/users.vue'
import orders from '../views/home/managementPage/orders.vue'
import traveling from '../views/home/travelingPage/traveling.vue'
import couponInfo from '../views/home/couponPage/couponInfo.vue'
import setUser from '../views/home/setUserPage/setUser.vue'
import pay from '../views/home/settingPage/pay.vue'
import homeBanner from '../views/home/settingPage/homeBanner.vue'
import foundation from '../views/home/settingPage/foundation.vue'
import feedback from '../views/home/feedbackPage/feedBack.vue'
import superior from '../views/home/managementPage/superior.vue'
import { useUserData } from '../stores/userData'
import  payRule  from '../views/home/settingPage/payRules.vue'
import service from '../views/home/servicePage/html/service.vue'
import report from '../views/home/reportPage/report.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect:"/login",
    },
    {
      path: '/login',
      name: 'login',
      component:login,
    },
    {
      path:'/admin',
      name:'admin',
      meta:"后台管理",
      beforeEnter(to, from,next) {
        const store = useUserData();
        if(!store.data.keyArray) next('/login');
        if (store.data?.keyArray.includes(to.name)) {
          next();
        } else {
          // 如果 to.name 不在 keyArray 中，可以进行重定向或者导航到其他页面
          if(store.data?.keyArray.length>0){
            next({name:store.data.keyArray[0]})
          }else{
            next('/login');
          }
          
        }
      },
      component:admin,
      redirect:'/admin/home/statistics',
      children:[
        {
          path:'home',
          name:'home',
          redirect:'/admin/home/statistics',
          meta:'首页',
          children:[
            {
              path:'statistics',
              name:'statistics',
              component:statistics,
              meta:'信息统计',
            },
            {
              path:'pending',
              name:'pending',
              component:pending,
              meta:"待处理事件"
            }
          ]
        },
        {
          path:'managment',
          name:'managment',
          redirect:'/admin/managment/product',
          meta:"管理",
          
          children:[
            {
              path:'product',
              name:'product',
              component:product,
              meta:"产品管理",
            },
            {
              path:'technician',
              name:'technician',
              component:technician,
              meta:"技师管理",
            },
            {
              path:'users',
              name:'users',
              component:users,
              meta:"用户管理",
            },
            {
              path:'orders',
              name:'orders',
              component:orders,
              meta:"订单管理",
            },
            {
              path:'superior',
              name:'superior',
              component:superior,
              meta:"代理管理",
            }
          ]
        },
        {
          path:'traveling',
          name:"traveling",
          component:traveling,
          meta:"交通出行"
        },
        {
          path:'coupon',
          name:'coupon',
          component:couponInfo,
          meta:"优惠卷活动",
        },
        {
          path:'setting',
          name:'setting',
          redirect:'/admin/setting/pay',
          meta:"系统设置",
          children:[
            {
              path:'pay',
              name:'pay',
              component:pay,
              meta:"支付设置"
            },
            {
              path:'homeBanner',
              name:'homeBanner',
              component:homeBanner,
              meta:"首页广告设置"
            },
            {
              path:'payRule',
              name:'payRule',
              component:payRule,
              meta:"退款规则设置"
            },
            {
              path:'foundaction',
              name:'foundaction',
              component:foundation,
              meta:"基础设置"
            }
          ]
        },
        {
          path:'setUser',
          name:"setUser",
          meta:"角色设置",
          component:setUser
        },
        {
          path:'feedBack',
          name:'feedBack',
          meta:"意见反馈",
          component:feedback
        },
        {
          path:'service',
          name:'service',
          meta:"客服服务",
          component:service
        },
        {
          path:'report',
          name:'report',
          component:report
        },
        {
          path: '/:pathMatch(.*)',
          name: 'not-found',
          redirect:'/admin'
        }


      ]
    },

  ]
})



export default router
